package snippets;

/* 
 * Canvas snippet: paint a circle in a canvas
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */

import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

public class Snippet245 {
	public static final int STEPS = 180;
	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				Rectangle rect = shell.getClientArea();
				draw(event.gc, rect);
//				event.gc.drawOval(0, 0, rect.width - 1, rect.height - 1);
			}
		});
		shell.setBounds(10, 10, 200, 200);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	protected static void draw(GC gc, Rectangle rect) {
		int[] points = new int[2*STEPS];
		int x0 = rect.width / 2;
		int y0 = rect.height / 2;
		for (int i=0 ; i<STEPS ; i++) {
			double angle = 2 * Math.PI * i / STEPS;
			double r = radius(angle);
			int x = (int)(x0 + x0*Math.cos(angle)*r/2);
			int y = (int)(y0 + y0*Math.sin(angle)*r/2);
			points[2*i] = x;
			points[2*i+1] = y;
		}
		gc.drawPolygon(points);
	}

	private static double radius(double angle) {
		double result = 1 
//			+ 0.2*Math.sin(2*angle) 
//			+ 0.2*Math.sin(3*angle)
			+ 0.2*Math.sin(4*angle)
		+ 0.2*Math.sin(5*angle)
		+ 0.1*Math.sin(10*angle)
			;
//		System.out.printf("a:%s r:%s%n", angle, result);
		return result;
	}
}
